package leo.gptBot.gpt;

//import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.MemoryId;
import dev.langchain4j.service.SystemMessage;
import dev.langchain4j.service.UserMessage;
import leo.gptBot.dto.request.Message;
import leo.gptBot.gpt.tools.MailTool;
import leo.gptBot.gpt.tools.WeatherTool;
import leo.gptBot.gpt.tools.MovieAndHotSearchTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author ：Leo
 * @date ：Created in 2024/7/22 19:09
 * @description：
 */
@Component
public class GPTClient {
    @Value("${openai.api-key}")
    private String apiKey;
    @Value("${openai.base-url}")
    private String baseUrl;
    @Autowired
    private PersistentChatMemoryStore chatMemoryStore;
    @Autowired
    private WeatherTool weatherTool;
    @Autowired
    private MovieAndHotSearchTool weiboHotSearchTool;
    @Autowired
    private MailTool mailTool;

    interface Assistant {
        @SystemMessage(
                "# ROLE # "
                + "You are a helpful assistant.And your name is Q-Copilot!"
                + "#############"
                + "# OBJECTIVE #"
                + "#############"
                +"Your objective is to simulate engaging and informative conversations with humans. "
                +"Respond to various queries, provide information, and assist with tasks while maintaining a friendly and approachable tone. "
                +"Ensure the interaction feels natural and helpful, enhancing the user's experience."
                + "#############"
                + "# SKILLS #"
                + "#############"
                +"•	Polite and friendly communication style and some small expressions can be used appropriately to enrich the chat."
                +"•	Problem-solving and information retrieval skills"
                +"•	In order to better serve users, there are tools for you to use and add your own opinions. For example, after obtaining the weather, you can give the user some dressing suggestions, etc."
        )

        String chat(@MemoryId String memoryId, @UserMessage String userMessage);
    }
    private Assistant model() {
        ChatMemoryProvider chatMemoryProvider = memoryId -> MessageWindowChatMemory.builder()
                .id(memoryId)
                .maxMessages(6)
                .chatMemoryStore(chatMemoryStore)
                .build();

        ChatLanguageModel model = OpenAiChatModel.builder()
                .apiKey(apiKey)
                .baseUrl(baseUrl)
                .modelName("gpt-4o-mini")
                .build();

        return AiServices.builder(Assistant.class)
                .chatLanguageModel(model)
                .tools(weatherTool,weiboHotSearchTool,mailTool)
                .chatMemoryProvider(chatMemoryProvider)
                .build();
    }

    public String chat(Message message){
        String id = message.isPrivateMsg()?message.getUserId().toString():message.getGroupId().toString();
        String msg = message.getRawMessage();
        return model().chat(id,msg);
    }
}

